<?php


namespace App\Http\Controllers\Base;
use App\Exceptions\ValidatorException;
use App\Http\Controllers\Controller;
use App\Http\Repositories\Repository;
use Illuminate\Http\Request;

class ImagesController extends Controller
{
    private $repository;
    public function __construct(Repository $repository)
    {
        $this->repository = $repository;
    }

    /**
     * 图片缩略图
     * @param Request $request
     * @return mixed
     * @throws ValidatorException
     */
    public function thumb(Request $request)
    {
        $rules = [
            'image' => 'required|image|max:1024',
        ];
        $messages = [

        ];
        $validator = \Validator::make($request->all(), $rules, $messages);
        if ($validator->fails()) {
            throw new ValidatorException($validator->errors()->all());
        }

        return $this->repository->thumb($request->all());
    }

    /**
     * 单图片上传
     * @param Request $request
     * @return mixed
     * @throws ValidatorException
     */
    public function uploadImage(Request $request)
    {
        $rules = [
            'image' => 'required|image|max:1024',
        ];
        $messages = [

        ];
        $validator = \Validator::make($request->all(), $rules, $messages);
        if ($validator->fails()) {
            throw new ValidatorException($validator->errors()->all());
        }

        return $this->repository->uploadImage($request->all());
    }

    /**
     * 多图片上传
     * @param Request $request
     * @return mixed
     * @throws ValidatorException
     */
    public function uploadImages(Request $request)
    {
        $rules = [
            'image' => 'required',
            'image.*' => 'image|max:1024',
        ];
        $messages = [

        ];
        $validator = \Validator::make($request->all(), $rules, $messages);
        if ($validator->fails()) {
            throw new ValidatorException($validator->errors()->all());
        }

        return $this->repository->uploadImage($request->all());
    }
}
